package com.huc.tools.tools.kafka;

import org.apache.kafka.clients.admin.AdminClient;
import org.apache.kafka.clients.admin.CreateTopicsResult;
import org.apache.kafka.clients.admin.NewTopic;
import org.apache.kafka.clients.consumer.ConsumerRecord;
import org.apache.kafka.clients.consumer.ConsumerRecords;
import org.apache.kafka.clients.consumer.KafkaConsumer;
import org.apache.kafka.clients.producer.KafkaProducer;
import org.apache.kafka.clients.producer.ProducerRecord;
import org.apache.kafka.clients.producer.RecordMetadata;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Properties;
import java.util.concurrent.Future;

/**
 * @Author: 胡成
 * @Version: 0.0.1V
 * @Date: 2018/8/13
 * @Description: kafka工具类
 **/
public class KafkaTools {
    /**
     * @Author: 胡成
     * @Date:   2018/8/13 13:25
     * @Description: 创建topic
    **/
    public static void createTopic(){
        //创建topic
        Properties props = new Properties();
        props.put("bootstrap.servers","192.168.0.115:9092");
        AdminClient adminClient = AdminClient.create(props);
        List<NewTopic> topics = new ArrayList<>();
        NewTopic newTopic = new NewTopic("topic-test",1,(short) 1);
        topics.add(newTopic);
        CreateTopicsResult result = adminClient.createTopics(topics);
        try{
            result.all().get();
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    /**
     * @Author: 胡成
     * @Date:   2018/8/13 13:26
     * @Description: 消费者
    **/
    public static void consumer(){
        Properties props = new Properties();
        props.put("bootstrap.servers","192.168.0.115:9092");
        props.put("group.id","test");
        props.put("enable.auto.commit","true");
        props.put("auto.commit.interval.ms","1000");
        props.put("key.deserializer", "org.apache.kafka.common.serialization.StringDeserializer");
        props.put("value.deserializer", "org.apache.kafka.common.serialization.StringDeserializer");
        KafkaConsumer<String,String> consumer = new KafkaConsumer<>(props);
        consumer.subscribe(Arrays.asList("topic-test"));
        while (true){
            ConsumerRecords<String,String> records = consumer.poll(100);
            for (ConsumerRecord<String,String> record:records){
                System.out.printf("offset=%d,key=%s,value=%s%n",record.offset(),record.key(),record.value());
            }
        }
    }

    /**
     * @Author: 胡成
     * @Date:   2018/8/13 13:26
     * @Description: 生产者
    **/
    public static void producer(){
        //创建producer
        Properties props = new Properties();
        props.put("bootstrap.servers","192.168.0.115:9092");
        props.put("acks","all");
        props.put("retries",0);
        props.put("batch.size",16384);
        props.put("linger.ms",1);
        props.put("buffer.memory",33554432);
        props.put("key.serializer", "org.apache.kafka.common.serialization.StringSerializer");
        props.put("value.serializer", "org.apache.kafka.common.serialization.StringSerializer");
//        org.apache.kafka.clients.producer.Producer<String,String> producer = new KafkaProducer<String, String>(props);
//        producer.send(new ProducerRecord<>("topic-test", "sdsds", "wewewe"));
//        producer.close();
        KafkaProducer<String, String> producer = new KafkaProducer<>(props);
        // 构造一个 Kafka 消息
        // 消息所属的Topic，请在控制台申请之后，填写在这里
        String topic = "topic-test";
        String key = "p1";
        // 消息的内容
        String value = "this is the message's value";
        ProducerRecord<String, String>  kafkaMessage = new ProducerRecord<>(topic, key,value);
        try {
            // 发送消息，并获得一个 Future 对象
            Future<RecordMetadata> metadataFuture = producer.send(kafkaMessage);
            // 同步获得 Future 对象的结果
            RecordMetadata recordMetadata = metadataFuture.get();
            System.out.println("Produce ok:" + recordMetadata.toString());
        } catch (Exception e) {
            //要考虑重试
            System.out.println("error occurred");
            e.printStackTrace();
        }

    }

    public static void main(String[] args) {
        producer();
    }
}
